package net.zjitc.cookiesession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author Administrator
 * @Date 2020/6/2 15:39
 * 服务端创建cookie，让cookie保存在客户端
 * 1、创建cookie
 * 2、发送cookie(把cookie响应给客户端)
 **/
@WebServlet("/cookieDemo1")
public class CookieDemo1 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*创建cookie对象，需要传入2个参数：name、value （ctrl+P）
        *  cookie其实就是键值对 */
        Cookie cookie=new Cookie("msg","haha");
        /*cookie默认是临时存储的(浏览器一旦关闭，就失效)
         可以设置cookie的存活时间*/
        cookie.setMaxAge(60*60);
        /* 发送cookie*/
        resp.addCookie(cookie);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
